/*
 * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Fetcher } from '@ahoo-wang/fetcher';

// Create a basic Fetcher instance
const fetcher = new Fetcher({
  baseURL: 'https://jsonplaceholder.typicode.com',
  timeout: 5000,
});
type Post = {
  id: string;
};
// Basic GET request
fetcher
  .get('/posts/1')
  .then((response: Response) => response.json<Post>())
  .then((data: Post) => {
    console.log('Basic GET request:', data);
  })
  .catch((error: unknown) => {
    console.error('Error:', error);
  });

// GET request with path parameters
fetcher
  .get('/posts/{id}', {
    urlParams: { path: { id: 1 } },
  })
  .then((response: Response) => response.json())
  .then((data: unknown) => {
    console.log('GET request with path params:', data);
  })
  .catch((error: unknown) => {
    console.error('Error:', error);
  });

// GET request with query parameters
fetcher
  .get('/posts', {
    urlParams: { query: { userId: 1 } },
  })
  .then((response: Response) => response.json())
  .then((data: unknown) => {
    console.log('GET request with query params:', data);
  })
  .catch((error: unknown) => {
    console.error('Error:', error);
  });
